package tag11_24_07.matrixAufgabe;

import java.util.Random;

public class IntMatrix {
	
	public static class BadDimensionException extends IllegalArgumentException {
		public BadDimensionException(String errMsg) {
			super(errMsg);
		}
	}
	public static class BadIndexException extends IllegalArgumentException {
		public BadIndexException(String errMsg) {
			super(errMsg);
		}
	}
	
	//throws BadDimensionException sollte man deklarieren und die javadoc erstellen
	static IntMatrix getRandomMatrix(int rows, int cols, int maxValue) {
		IntMatrix m = new IntMatrix(rows, cols);
		
		Random random = new Random();
		
		for (int i = 0; i < m.getRowsCount(); i++) {
			for (int j = 0; j < m.getColsCount(); j++) {
				
				int zufallszahl = random.nextInt(maxValue+1);
				m.data[i][j] = zufallszahl;
			}
		}
		
		return m;
	}
	
	
	//----------------------------------------------------------
	private int[][] data;

	/**
	 * Erzeugt eine Matrix rows X cols 
	 * 
	 * @param rows
	 * @param cols
	 * @throws BadDimensionException - bei rows < 1 oder cols < 1 
	 */
	IntMatrix(int rows, int cols) throws BadDimensionException {
		if(rows<1 || cols<1) {
//			System.out.println("Fehler im Konstruktor der IntMatrix. Falsche Dimensionen: " + rows + " X " + cols);
//			System.exit(0);
			throw new BadDimensionException("Fehler im Konstruktor der IntMatrix. Falsche Dimensionen: " + rows + " X " + cols);
		}
			
		data = new int[rows][cols]; 
	}

	/**
	 * 
	 * @param rows
	 * @param cols
	 * @param initValue
	 * @throws BadDimensionException
	 */
	IntMatrix(int rows, int cols, int initValue) throws BadDimensionException {
		this(rows, cols); //Aufruf des Konstruktors (int, int)
		
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[i].length; j++) {
				data[i][j] = initValue;
			}
		}		
	}
	
	/**
	 * 
	 * @param row 	0 ... Anzahl Zeilen - 1
	 * @param col   0 ... Anzahl Spalten - 1
	 */
	int get(int row, int col) {
		if( row < 0 || row >= getRowsCount() ) {
			String msg = "Fehler! Falscher row-Index: " + row 
					+ ". Erlaubt: 0 ... " + (getRowsCount() - 1);
			throw new BadIndexException(msg);
		}
		
		if( col < 0 || col >= getColsCount() ) {
			String errMsg = "Fehler! Falscher col-Index: " + col 
					+ ". Erlaubt: 0 ... " + (getColsCount() - 1);
			throw new BadIndexException(errMsg);
		}
		
		return data[row][col];
	}
	
	int getRowsCount() {
		return data.length;
	}
	
	int getColsCount() {
		return data[0].length;
	}
	
	public String toString() {
		
		String back = "";
		
		for(int i=0; i<data.length ; i++) {
			for(int j=0; j<data[i].length; j++) {
				back = back + data[i][j] + ", ";
			}
			
			if( back.endsWith(", ") ) {
				back = back.substring(0, back.length()-2);
			}
			
			back += "\n";
		}
		
		return back;
	}
	

	boolean equals(IntMatrix m) {
		
		if( getRowsCount() != m.getRowsCount() )
			return false;
		
		if( getColsCount() != m.getColsCount() )
			return false;
		
		for (int i = 0; i < getRowsCount(); i++) {
			for (int j = 0; j < getColsCount(); j++) {
				if( get(i, j) != m.get(i, j) ) {
					return false;
				}
			}
		}
		
		return true;
	}
	
}













